package drds.server.sqlparser.sqlobject.expression.binary;

import java.io.Serializable;

import drds.server.sqlparser.sqlobject.expression.AbstractExpression;
import drds.server.sqlparser.sqlobject.expression.Expression;
import drds.server.sqlparser.visitor.Visitor;

/**
 * 
 * @author 曹正辉<330937205@qq.com>
 * 
 */
public class BinaryExpression extends AbstractExpression implements Serializable {

	private static final long serialVersionUID = 1L;
	private Expression left;
	private BinaryOperator binaryOperator;
	private Expression right;

	public BinaryExpression() {

	}

	public BinaryExpression(Expression left, BinaryOperator binaryOperator, Expression right) {
		setLeft(left);
		this.binaryOperator = binaryOperator;
		setRight(right);
	}

	public BinaryExpression(Expression left, Expression right, BinaryOperator binaryOperator) {

		setLeft(left);
		this.binaryOperator = binaryOperator;
		setRight(right);
	}

	public void setLeft(Expression left) {
		if (left != null) {
			left.setParent(this);
		}
		this.left = left;
	}

	public Expression getLeft() {
		return this.left;
	}

	public void setBinaryOperator(BinaryOperator binaryOperator) {
		this.binaryOperator = binaryOperator;
	}

	public BinaryOperator getBinaryOperator() {
		return this.binaryOperator;
	}

	public void setRight(Expression right) {
		if (right != null) {
			right.setParent(this);
		}
		this.right = right;
	}

	public Expression getRight() {
		return this.right;
	}

	protected void accept0(Visitor visitor) {
		if (visitor.visit(this)) {
			acceptChild(visitor, this.left);
			acceptChild(visitor, this.right);
		}

		visitor.endVisit(this);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((left == null) ? 0 : left.hashCode());
		result = prime * result + ((binaryOperator == null) ? 0 : binaryOperator.hashCode());
		result = prime * result + ((right == null) ? 0 : right.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (o == null) {
			return false;
		}
		if (!(o instanceof BinaryExpression)) {
			return false;
		}
		BinaryExpression other = (BinaryExpression) o;
		if (left == null) {
			if (other.left != null) {
				return false;
			}
		} else if (!left.equals(other.left)) {
			return false;
		}
		if (binaryOperator != other.binaryOperator) {
			return false;
		}
		if (right == null) {
			if (other.right != null) {
				return false;
			}
		} else if (!right.equals(other.right)) {
			return false;
		}
		return true;
	}

}
